<?php
/*首页数据*/
namespace Home\Model;
use Think\Model;
class IndexModel extends Model {
    const SHOW_HOT_COUNT=6;
    const SHOW_NEW_COUNT=6;
    const SHOW_PROMO_COUNT=10;
    const SHOW_PAGE_COUNT=5;
    public function getCarData($cityName){
        //获取城市
        $city=M('city');
        $where="name='".$cityName."' and is_show=1";
        $field="id";
        $city_info=$city->field($field)->where($where)->find();
        $carnums='';

        $car=M('cars');
        //获取促销车型
        $where="is_promo > 0 and is_show=1";
        $order="is_promo asc";
        $field="*";
        $promo_car=$car->field($field)->where($where)->order($order)->limit(0,self::SHOW_PROMO_COUNT)->select();
        foreach($promo_car as $val){
            $carnums.=$carnums?','.$val['carnum']:$val['carnum'];
        }
        //获取热销车型
        $where="is_hot > 0  and is_show=1";
        $order="is_hot asc";
        $field="*";
        $hot_car=$car->field($field)->where($where)->order($order)->limit(0,self::SHOW_HOT_COUNT)->select();
        foreach($hot_car as $val){
            $carnums.=$carnums?','.$val['carnum']:$val['carnum'];
        }
        //获取新品车型
        $where="is_new > 0  and is_show=1";
        $order="is_new asc";
        $field="*";
        $new_car=$car->field($field)->where($where)->order($order)->limit(0,self::SHOW_NEW_COUNT)->select();
        foreach($new_car as $val){
            $carnums.=$carnums?','.$val['carnum']:$val['carnum'];
        }
        //获取价格
        $price=M('price');
        $field="carnum,month_pay";
        if($carnums){
            $where="carnum in (".$carnums.") and purchase_scheme=1 and is_new=1 and city_id=".$city_info['id'];
        }else{
            $where="carnum in ('') and purchase_scheme=1 and is_new=1 and city_id=".$city_info['id'];
        }
        $price_infos=$price->field($field)->where($where)->select();
        foreach($price_infos as $val){
            $price_data[$val['carnum']]=$val['month_pay'];
        }

        foreach($promo_car as $key=>$val){
            $promo_car[$key]['month_pay']=$price_data[$val['carnum']];
        }
        foreach($hot_car as $key=>$val){
            $hot_car[$key]['month_pay']=$price_data[$val['carnum']];
        }
        foreach($new_car as $key=>$val){
            $new_car[$key]['month_pay']=$price_data[$val['carnum']];
        }
        $data=array();
        $data['promo_car']=$promo_car;
        $data['hot_car']=$hot_car;
        $data['new_car']=$new_car;

        $car_baike = M('car_baike');
        $baike_res = $car_baike->field('id,title') -> where('is_show=1') ->limit(0,5)-> select();
        $data['car_baike']=$baike_res;

        $broadcast = M('broadcast');
        $broadcast_res = $broadcast->field('id,tel,type')-> select();
        shuffle($broadcast_res);
        $data['broadcast']=$broadcast_res;
        
        return $data;
    }
    public function getDetailsDate($carnum){
         $details = M('car_details');      
         $where="carnum=".$carnum;
         $field=" * ";
         $base=$details->field($field)->where($where)->find();
         return $base;

    }  
    public function getCarPrice($carnum,$cityid){
        

         $price = M('price');      
         $where="carnum=".$carnum." and is_new=1 and city_id=".$cityid;
         $order="purchase_scheme asc";
         $field=" * ";
         $base=$price->field($field)->where($where)->select();
         return $base;

    }
     public function getCarsDate($carnum){
         $cars = M('cars');      
         $where="carnum=".$carnum;
         $field=" * ";
         $detail=$cars->field($field)->where($where)->find();
         return $detail;

    } 

    public function getCarsRecommend($carsData,$cityid){
         $cars = M('cars');      
         $where="type='".$carsData['type']."' and is_show=1 and carnum!='".$carsData['carnum']."'";
         $field=" * ";
         $car_data_type=$cars->field($field)->where($where)->limit(0,12)->select();
         $carnums='';
         if(count($car_data_type)<6){    
            $where="type!='".$carsData['type']."' and is_show=1";
            $field=" carnum ";
            $car_rand=$cars->field($field)->where($where)->select();
            $count=6-count($car_data_type);
            $car_rand_key=array_rand($car_rand,$count);
            $carnum_arr=[];
            foreach($car_rand_key as $v){
                $carnum_arr[]=$car_rand[$v]['carnum'];
            }
            $where_rand['carnum']=array('in',$carnum_arr);
            $field=" * ";
            $car_data_rand=$cars->field($field)->where($where_rand)->select();
            if(count($car_data_type)>0 && count($car_data_rand)>0){
                $carsDatas = array_merge($car_data_type,$car_data_rand);
            }else{
                $carsDatas = count($car_data_type)>0?$car_data_type:$car_data_rand;
            }
         }else{
             $car_rand_key=array_rand($car_data_type,6);
             foreach($car_rand_key as $v){
                $carsDatas[]=$car_data_type[$v];
             }
         }
         foreach($carsDatas as $val){
            $carnums.=$carnums?','.$val['carnum']:$val['carnum'];
         }
        //获取价格
        $price=M('price');
        $field="carnum,month_pay";
        if($carnums){
            $where="carnum in (".$carnums.") and purchase_scheme=1 and is_new=1 and city_id=".$cityid;
        }else{
            $where="carnum in ('') and purchase_scheme=1 and is_new=1 and city_id=".$cityid;
        }
        $price_infos=$price->field($field)->where($where)->select();
        foreach($price_infos as $val){
            $price_data[$val['carnum']]=$val['month_pay'];
        }
        foreach($carsDatas as $key=>$val){
            $carsDatas[$key]['month_pay']=$price_data[$val['carnum']];
        }
         return $carsDatas;
    } 
    public function getCarList(){
         $cars = M('cars');      
         $where=" is_show=1";
         $field=" name ";
         $carsData=$cars->field($field)->where($where)->select();
         $carList=[];
         foreach($carsData as $val){
             $carList[]=$val['name'];
         }
         return $carList;

    } 
    /*
        功能:按查询条件获取新车数据
    */
    public function getNewData($where, $order, $page) {
        $index = ($page-1)*self::SHOW_PAGE_COUNT;    
        
        //获取符合条件的新车信息
        $car = M('cars');
        $field = "jdc_cars.carnum,jdc_cars.name,jdc_cars.details,jdc_cars.guide_price,jdc_price.month_pay";
        //计算最大页数
        //1.结果总数
        $count = $car->join("jdc_price on jdc_cars.carnum=jdc_price.carnum")->where($where)->count();
        //进行查询
        $new_car = $car->join("jdc_price on jdc_cars.carnum=jdc_price.carnum")->field($field)->where($where)->order($order)->limit($index,self::SHOW_PAGE_COUNT)->select();
        //最大页数
        $maxPage = ceil($count/self::SHOW_PAGE_COUNT);
        $data = array();
        $data['new_car'] = $new_car;
        $data['maxPage'] = $maxPage;
        return $data;        
    }

    public function getUserOrder($tel){
         $mode  = M('user');
         $where="tel='".$tel."'";
         $userData = $mode->where($where)->find();
         $orderModle = M('order');      
         $where_order="uid=".$userData['id']." and status>=0";
         $field="*";
         $order="create_time desc";
         $orderData=$orderModle->field($field)->where($where_order)->order($order)->find();
         
         if($orderData['status']==6){
             $order_operation = M('order_operation');
             $where_operation="orderid='".$orderData['orderid']."' and status=6";
             $field=" create_time ";
             $orderOperationData=$order_operation->field($field)->where($where_operation)->find();
             if($orderOperationData){
                  $orderData['finish_time']=date("Y-m-d H:i:s",$orderOperationData['create_time']);
             }
            

             $order_month_pay = M('order_month_pay');
             $where_pay="orderid='".$orderData['orderid']."' and status=0";
             $field=" * ";
             $order="pay_time asc";
             $orderMonthPayData=$order_month_pay->field($field)->where($where_pay)->find();
             
             if($orderMonthPayData){
                 $orderData['last_pay_time']=date("Y-m-d H:i:s",$orderMonthPayData['pay_time']);
                 if($orderMonthPayData['pay_time'] -time()<=10*24*60*60 && $orderMonthPayData['status']=='0'){
                     if($orderMonthPayData['next_pay_time'] -time()>=0){
                         $orderMonthPayData['next_pay_days']=ceil(($orderMonthPayData['pay_time'] -time())/(24*60*60));
                     }else{
                         $orderMonthPayData['next_pay_days']=0-ceil((time()-$orderMonthPayData['pay_time'])/(24*60*60));
                     }
                     $orderMonthPayData['next_pay']=$orderMonthPayData['money'];
                     $orderMonthPayData['pay_month']=$orderMonthPayData['pay_month']-1;
                     $orderMonthPayData['paymoney']=$orderMonthPayData['pay_month']*$orderMonthPayData['money'];
                     $orderMonthPayData['banknum']=substr($userData['banknum'],-4);
                     $data['orderPay']=$orderMonthPayData;
                 }
             }else{
                $orderData['last_pay_time']=''; 
             }
             
            
             
         }
        $order_operation = M('order_operation');
        $where_operation="orderid='".$orderData['orderid']."' and status=0";
        $field=" usernum ";
        $orderOperationData=$order_operation->field($field)->where($where_operation)->find();
        if($orderOperationData){
            $admin = M('admin');
            $where_admin="usernum='".$orderOperationData['usernum']."'";
            $field=" tel ";
            $admin=$admin->field($field)->where($where_admin)->find();
            if($admin){
                $orderData['tel']=$admin['tel'];
            }
                
         }

         $cars = M('cars');
         $where_car="carnum='".$orderData['carnum']."'";
         $field=" name,details,jdc_price,guide_price,first_pay,month_pay ";
         $carData=$cars->field($field)->where($where_car)->find();
         if($carData){
             $orderData['carname']=$carData['name'].' '.$carData['details'];
             $orderData['guide_price']=$carData['guide_price'];
             $orderData['first_pay']=$carData['first_pay'];
             $orderData['month_pay']=$carData['month_pay'];
             $orderData['jdc_price']=$carData['jdc_price'];
         }

         $carsColor = M('cars_stock');
         $where_color="id='".$orderData['stockid']."'";
         $field=" color ";
         $carColorData=$carsColor->field($field)->where($where_color)->find();
         if($carColorData){
              $orderData['color']=$carColorData['color'];
         }
         $data['order']=$orderData;
         return $data;

    } 
    public function setUserOrder($order){
         $orderModle = M('order');      
         $where_order="orderid='".$order."'";
         $orderData['status']=6;
         $orderModle->where($where_order)->save($orderData);

         $orderOperation = M('order_operation');      
         $orderOperationData['orderid']=$order;
         $orderOperationData['status']=6;
         $orderOperationData['create_time']=time();
         $orderOperation->add($orderOperationData);
    }
    public function getOrderPay($tel){
         $mode  = M('user');
         $where="tel='".$tel."'";
         $userData = $mode->where($where)->find();
         $orderPayModle = M('order_pay');      
         $where_order_pay="uid=".$userData['id']." and status = 1";
         $field="*";
         $order="id desc";
         $orderPayData=$orderPayModle->field($field)->where($where_order_pay)->order($order)->select();
         if($orderPayData){
            foreach($orderPayData as $key=>$val){
                $orderPayData[$key]["create_time"]=date("Y-m-d H:i:s",$val['create_time']);
                if($val['pay_month']>0){
                    $orderPayData[$key]["paymoney"]=$val['pay_month']*$val['money'];
                }
                $orderPayData[$key]["create_time"]=date("Y-m-d H:i:s",$val['create_time']);
            }
         }

         $order_month_pay = M('order_month_pay');
         $where_pay="orderid='".$orderData['orderid']."' and status=0";
         $field=" * ";
         $order="pay_time asc";
         $orderMonthPayData=$order_month_pay->field($field)->where($where_pay)->find();
         if($orderMonthPayData){
            $orderData['last_pay_time']=date("Y-m-d H:i:s",$orderMonthPayData['pay_time']);
            if($orderMonthPayData['pay_time'] -time()<=10*24*60*60 && $orderMonthPayData['status']=='0'){
                if($orderMonthPayData['next_pay_time'] -time()>=0){
                    $orderMonthPayData['next_pay_days']=ceil(($orderMonthPayData['pay_time'] -time())/(24*60*60));
                }else{
                    $orderMonthPayData['next_pay_days']=0-ceil((time()-$orderMonthPayData['pay_time'])/(24*60*60));
                }
                $orderMonthPayData['next_pay']=$orderMonthPayData['money'];
                $orderMonthPayData['pay_month']=$orderMonthPayData['pay_month']-1;
                $orderMonthPayData['paymoney']=$orderMonthPayData['pay_month']*$orderMonthPayData['money'];
                $orderMonthPayData['banknum']=substr($userData['banknum'],-4);
                $data['next_pay']=$nextPayData;
              }
         }else{
            $orderData['last_pay_time']=''; 
         }   
         $data['order_pay']=$orderPayData;
         return $data;

    } 
}
?>